package rocketmq.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @author: Dragon
 * @since: 2023/12/3 16:47
 */
@Slf4j
public class RocketMQConsumer {
    public static void main(String[] args) throws Exception {

        try {
            DefaultMQPushConsumer dragonConsumerGroup = new DefaultMQPushConsumer("dragonConsumerGroup");
            dragonConsumerGroup.setNamesrvAddr("127.0.0.1:9876");
            dragonConsumerGroup.subscribe("dragonTopic", "*");
            dragonConsumerGroup.registerMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {

                    System.out.println("消费者收到的消息为：" + list);
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            });
            dragonConsumerGroup.start();
            System.out.println("消费者启动成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
